package com.mockWriter;

import static org.mockito.Mockito.*;

import java.io.Writer;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;

public class MyServiceTest {

    @Test
    void testProcessSkipsActualFileWrite() throws IOException {
        try (MockedConstruction<FileWriter> mockFileWriter = mockConstruction(FileWriter.class,
                (mock, context) -> {
                    // 可以钩住行为，例如记录调用、做断言等。
                })) {

            MyService service = new MyService();
            service.process();

            // 获取构造出的 mock 实例
            FileWriter mockWriter = mockFileWriter.constructed().get(0);

            // 验证 write("Hello, world!") 被调用
            verify(mockWriter).write("Hello, world!");
            // 验证 close() 被调用
            verify(mockWriter).close();
        }
    }
}
